package com.smedi.ismedi.constructionproject.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.Person;
import com.smedi.ismedi.constructionproject.core.domain.dto.PersonQuery;
import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.constructionproject.core.port.adapter.restful.convert.AdapterConverter;
import com.smedi.ismedi.constructionproject.core.port.adapter.service.PersonnelRemoteApiService;
import com.smedi.ismedi.personnel.api.dto.request.PersonListQueryRequest;
import com.smedi.ismedi.personnel.api.dto.response.PersonSimpleResponse;
import com.smedi.ismedi.personnel.api.dto.response.resp.PersonResp;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.personnel.api.feign.RemotePersonValidateFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-10 20:36
 */
@Slf4j
@Service
@AllArgsConstructor
public class PersonnelRemoteApiServiceImpl implements PersonnelRemoteApiService {
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    private final RemotePersonValidateFeignApi remotePersonValidateFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public Boolean checkPersonIsAvailable(Long personId) {
        ResponseData<Boolean> response = remotePersonValidateFeignApi.getCheckPersonAvailable(personId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkPersonIsInternal(Long personId) {
        ResponseData<Boolean> response = remotePersonValidateFeignApi.getCheckPersonIsInternal(personId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getPersonIdsByLastNameAndUserNameAndEmployeeNumAndMobile(String lastName, String userName, String employeeNum, String mobile) {
        ResponseData<List<Long>> response = remotePersonCoreFeignApi.getQueryPersonIdsByLastNameAndUserNameAndEmployeeNumAndMobile(lastName, userName, employeeNum, mobile);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<PersonSimpleResponse> getPersonSimpleInfoByPersonIds(List<Long> personIds) {
        ResponseData<List<PersonSimpleResponse>> response = remotePersonCoreFeignApi.getQueryPersonSimpleList(personIds);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }
    @Override
    public List<Person> getPersonList(PersonQuery personQuery) {
        PersonListQueryRequest personListQueryRequest = adapterConverter.personQueryToPersonListQueryRequest(personQuery);
        ResponseData<List<PersonResp>> responseData = remotePersonCoreFeignApi.getPersonList(personListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return adapterConverter.respToPerson(responseData.getData());
    }
}
